package com.ruoyi.xzs.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.xzs.domain.Question;
import com.ruoyi.xzs.excelTemplate.QuestionImportTemplate;
import com.ruoyi.xzs.service.IQuestionService;
import com.ruoyi.xzs.vo.QuestionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 题库管理Controller
 * 
 * @author li
 * @date 2023-03-08
 */
@RestController
@RequestMapping("/question")
public class QuestionController extends BaseController
{
    @Autowired
    private IQuestionService questionService;

    /**
     * 查询题库管理列表
     */
    @RequiresPermissions("xzs:question:list")
    @GetMapping("/list")
    public TableDataInfo list(Question question)
    {
        startPage();
        List<QuestionVO> list = questionService.selectQuestionList(question);
        return getDataTable(list);
    }

    /**
     * 导出excel试题模板
     */
    @RequiresPermissions("xzs:question:export")
    @Log(title = "试题模板导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        ExcelUtil<QuestionImportTemplate> util = new ExcelUtil<QuestionImportTemplate>(QuestionImportTemplate.class);
        util.exportExcel(response, null, "模板");
    }

    /**
     * 试题excel导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "试题导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("xzs:question:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<QuestionImportTemplate> util = new ExcelUtil<QuestionImportTemplate>(QuestionImportTemplate.class);
        List<QuestionImportTemplate> quationList = util.importExcel(file.getInputStream());
        logger.info("questionList；",quationList);
        String operName = SecurityUtils.getUsername();
        String message = questionService.importUser(quationList, updateSupport, operName);
        return success(message);
    }

    /**
     * 获取题库管理详细信息
     */
    @RequiresPermissions("xzs:question:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(questionService.selectQuestionById(id));
    }

    /**
     * 新增题库管理
     */
    @RequiresPermissions("xzs:question:add")
    @Log(title = "题库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Question question)
    {
        return toAjax(questionService.insertQuestion(question));
    }

    /**
     * 修改题库管理
     */
    @RequiresPermissions("xzs:question:edit")
    @Log(title = "题库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Question question)
    {
        return toAjax(questionService.updateQuestion(question));
    }

    /**
     * 删除题库管理
     */
    @RequiresPermissions("xzs:question:remove")
    @Log(title = "题库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(questionService.deleteQuestionByIds(ids));
    }
}
